﻿using A.M.ETO;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MiniExcelLibs;
using Redis_ClientInfor;
using W.M.Materiel.Application.IServices;
using W.M.Materiel.Domain.Entity;
using W.M.Materiel.Infrastruecure.DTO;
using W.M.Materiel.Infrastruecure.Repository;

namespace W.M.Materiel.Read.API.Controllers
{
    /// <summary>
    /// 物料清单管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class Bill_of_materialsController : ControllerBase
    {
        private readonly IBill_of_materialsServices bill_Of_MaterialsServices;
        

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="bill_Of_MaterialsServices"></param>
        public Bill_of_materialsController(IBill_of_materialsServices bill_Of_MaterialsServices)
        {
            this.bill_Of_MaterialsServices = bill_Of_MaterialsServices;
        }

        /// <summary>
        /// 测试异常信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult getNlog()
        {
            int a = 10;
            int b = 0;
            return Ok(a / b);
        }

        /// <summary>
        /// 物料信息+查询+分页
        /// </summary>
        /// <param name="pageSize">页容量</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="name">查询条件：物料名称</param>
        /// <param name="id">查询条件：单位id</param>
        /// <returns>物料信息</returns>
        [HttpGet]
        public IActionResult GetBill_Of_Materials(int pageSize, int pageIndex, string? name, string? id)
        {
            int totalCount;
            int pageCount;
            var list = bill_Of_MaterialsServices.GetBill_Of_Materials(pageSize, pageIndex, name, id,out totalCount,out pageCount);
            return Ok(new
            {
                totalCount,pageCount,list
            });
        }

        /// <summary>
        /// 反填BOM配件信息
        /// </summary>
        /// <param name="material_code">物料编号</param>
        /// <returns>BOM配件数据</returns>
        [HttpGet]
        public async Task<IActionResult> Getbackfill(string? material_code)
        {
            var list= await bill_Of_MaterialsServices.Getbackfill(material_code);
            return Ok(list);
        }

        /// <summary>
        /// 添加物料信息u
        /// </summary>
        /// <param name="c">物料实体</param>
        /// <returns>统一返回格式</returns>
        [HttpPost]
        public async Task<IActionResult> GetBill_Of_MaterialAdd(bill_of_materials c)
        {
            var list = await bill_Of_MaterialsServices.GetBill_Of_MaterialAdd(c);
            return Ok(list);
        }

        /// <summary>
        /// 删除物料信息
        /// </summary>
        /// <param name="c">物料编号</param>
        /// <returns>统一返回格式</returns>
        [HttpPost]
        public async Task<IActionResult> GetDelBill(string? c)
        {
            var list=await bill_Of_MaterialsServices.GetDelBill(c);
            return Ok(list);
        }

        /// <summary>
        /// 修改物料信息
        /// </summary>
        /// <param name="c">物料实体</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> GetUpdateBillInfo(bill_of_materials c)
        {
            var list=await bill_Of_MaterialsServices.GetUpdateBillInfo(c);
            return Ok(list);
        }

        /// <summary>
        /// 反填物料数据
        /// </summary>
        /// <param name="material_code">物料编号</param>
        /// <returns>物料数据</returns>
        [HttpGet]
        public async Task<IActionResult> GetBillbackfill(string? material_code)
        {
            var list =await bill_Of_MaterialsServices.GetBillbackfill(material_code);
            return Ok(list);
        }

        /// <summary>
        /// 物料导出
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult BillExportTiExcel()
        {
            var list = bill_Of_MaterialsServices.GetShowInfor();
            var steram = new MemoryStream();
            steram.SaveAs(list);
            steram.Seek(0, SeekOrigin.Begin);
            return new FileStreamResult(steram, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                FileDownloadName = "物料信息.xlsx"
            };
        }

        ///// <summary>
        ///// Redis测试
        ///// </summary>
        ///// <returns></returns>
        //[Route("Redis")]
        //[HttpGet]
        //public ActionResult Redis()
        //{
        //    var redisResult = RedisClient.redisClient.SetStringKey("mys","666");
        //    return Ok(new { code = "200", res = new { msg = redisResult.ToString() } });
        //}
    }
}
